package javabean;

import java.io.*;
import java.sql.*;

import javabean.DataBaseConnection;

public class CreateSetterGetter{
	
	//单词首字母小写换为大写
	public  String transWord(String word){
		String newword =word.replace(word.charAt(0), (char)(word.charAt(0)-32));
		return newword ;
	}
	//根据数据库表列的类型定义返回相应的java变量类型
	public String reTypeStr(String typename){
		String typeStr = "" ;
		if("varchar".equals(typename)||"char".equals(typename)||"nchar".equals(typename)||"nvarchar".equals(typename)||"text".equals(typename)||"ntext".equals(typename)){
			typeStr = "String" ;
		}
		else if("smallint".equals(typename)||"int".equals(typename)||"tinyint".equals(typename)){
			typeStr = "int" ;
		}
		else if("bigint".equals(typename)){
			typeStr = "long" ;
		}
		else if("numeric".equals(typename)){
			typeStr = "double" ;
		}
		else if("float".equals(typename)){
			typeStr = "float" ;
		}
		return typeStr ;
	}
	
	//新建生成Setter,Getter文件
	public boolean createSG(String tablename,String dir,String foldername,String filename,String filetype) throws Exception{
		boolean tag = false ;
		DataBaseConnection dbc = new DataBaseConnection() ;
		Connection conn = null ;
		ResultSet rs = null ;

		try {
			conn = dbc.getConnection() ;
			rs =  conn.prepareStatement("SELECT * FROM "+tablename).executeQuery();
			ResultSetMetaData meta = rs.getMetaData() ;			//返回rs的列信息(列名，列类型等)
			int count = meta.getColumnCount() ;					//返回列的总数
			if(count>0){
				//新建文件夹和文件
				File fdir = new File(dir,foldername);
				fdir.mkdir() ;
				File f = new File(fdir,filename+filetype) ;
				//创建输出流
				FileWriter outfile = new FileWriter(f) ;
				BufferedWriter bufferout = new BufferedWriter(outfile) ;
				//写入头数据
				bufferout.write("public class "+filename+"{" ) ;
				bufferout.newLine() ;
				bufferout.newLine() ;
				String temp1 = "" ;
				String temp2 = "" ;
				String temp3 = "" ;
				
				for(int i=1;i<=count;i++){				
					temp1 = "private "+reTypeStr(meta.getColumnTypeName(i))+" "+meta.getColumnName(i)+" ;" ;
					//将字符串写入文件
					bufferout.write(temp1);
					//换行
					bufferout.newLine() ;
				}
				bufferout.newLine() ;
				String colname = "" ;
				String typename = "" ;
				for(int j=1;j<=count;j++){
					colname = meta.getColumnName(j) ;
					typename = meta.getColumnTypeName(j) ;
					temp2 = "public void set"+transWord(colname)+"("+reTypeStr(typename)+" "+colname+"){"+
							"this."+colname+" = "+colname+";}" ;
					temp3 = "public "+reTypeStr(typename)+" get"+transWord(colname)+"(){"+
					"return this."+colname+";}";
					
					bufferout.write(temp2);
					bufferout.newLine() ;
					bufferout.write(temp3) ;
					bufferout.newLine() ;
					bufferout.newLine() ;
				}
				bufferout.write("}") ;
				bufferout.close() ;
				tag = true ;
				rs.close() ;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			conn.close() ;
		}
		return tag;
	}

	
}
